﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess.DataSetMainTableAdapters;

namespace DataAccess
{
    public class DBMSAccess
    {

        SumpPumpDataTableAdapter sumpPumpDataTableAdapter =
            new SumpPumpDataTableAdapter();

        SumpPumpEventTableAdapter sumpPumpEventTableAdapter =
            new SumpPumpEventTableAdapter();

        SumpPumpMonitorConfigurationTableAdapter sumpPumpMonitorConfigrationTableAdapter =
            new SumpPumpMonitorConfigurationTableAdapter();

        NetControlToolsServiceConfigurationTableAdapter configAdapter =
            new NetControlToolsServiceConfigurationTableAdapter();

        public DataSetMain CurrentDataSet { get; set; }

        public DBMSAccess()
        {
            CurrentDataSet = new DataSetMain();

            configAdapter.Fill(CurrentDataSet.NetControlToolsServiceConfiguration);

            //sumpPumpDataTableAdapter.Fill(CurrentDataSet.SumpPumpData);

            sumpPumpMonitorConfigrationTableAdapter.Fill(
                CurrentDataSet.SumpPumpMonitorConfiguration);
        }

        public int heartbeatIntervalMilliseconds()
        {
            if (CurrentDataSet != null)
            {
                configAdapter.Fill(CurrentDataSet.NetControlToolsServiceConfiguration);
                return CurrentDataSet.NetControlToolsServiceConfiguration[0].
                    HeartbeatIntervalMilliseconds;
            }
            else
            {
                return 1000 * 60;
            }
        }

        public int ConfigurationUpdateInterval()
        {
            if (CurrentDataSet != null)
            {
                configAdapter.Fill(CurrentDataSet.NetControlToolsServiceConfiguration);
                return CurrentDataSet.NetControlToolsServiceConfiguration[0].
                    CheckForServiceConfigurationUpdatesIntervalMilliseconds;
            }
            else
            {
                return 1000 * 60;
            }
        }

        public int ServicePollingDeviceIntervalMilliseconds()
        {
            if (CurrentDataSet != null)
            {
                sumpPumpMonitorConfigrationTableAdapter.Fill(
                    CurrentDataSet.SumpPumpMonitorConfiguration);
                return CurrentDataSet.SumpPumpMonitorConfiguration[0].
                    PollFrequencyMilliseconds;
            }
            else
            {
                return 1000 * 60;
            }
        }

        public string sourceEmailAddress()
        {
            if (CurrentDataSet != null)
            {
                return CurrentDataSet.NetControlToolsServiceConfiguration[0].
                    AddressToSendEmailFrom;
            }
            else
            {
                return "Santa@NorthPole.com";
            }
        }

        public string destinationEmailAddressList()
        {
            if (CurrentDataSet != null)
            {
                return CurrentDataSet.NetControlToolsServiceConfiguration[0].
                    CommaSeperatedAddressesToSendEmailTo;
            }
            else
            {
                return "Santa@NorthPole.com";
            }
        }

        public bool emailIsConfiguredAndShouldBeSent()
        {
            if (CurrentDataSet != null)
            {
                string recipients = destinationEmailAddressList();
                return (!String.IsNullOrEmpty(recipients));
            }
            else
            {
                return false;
            }
        }

        public void writeSumpPumpData(DateTime EventDateTime, int RawDistance)
        {
            writeSumpPumpData(EventDateTime, RawDistance, -99, string.Empty);
        }
        public void writeSumpPumpData(DateTime EventDateTime, int RawDistance, int CalibratedWaterLevel, string comments)
        {
            sumpPumpDataTableAdapter.Insert(Guid.NewGuid()
                   , EventDateTime
                   , RawDistance
                   , CalibratedWaterLevel
                   , comments);
            //CurrentDataSet.SumpPumpData.AddSumpPumpDataRow(Guid.NewGuid()
            //    , EventDateTime
            //    , RawDistance
            //    , CalibratedWaterLevel
            //    , comments);
            //sumpPumpDataTableAdapter.Update(CurrentDataSet.SumpPumpData);
        }

        public void writeSumpPumpEvent(DateTime EventDateTime, string eventType, string eventDescription)
        {
            sumpPumpEventTableAdapter.Insert(Guid.NewGuid()
                , EventDateTime
                , eventType
                , eventDescription);
            //CurrentDataSet.SumpPumpEvent.AddSumpPumpEventRow(Guid.NewGuid()
            //    , EventDateTime
            //    , eventType
            //    , eventDescription);
            //sumpPumpEventTableAdapter.Update(CurrentDataSet.SumpPumpEvent);
        }
    }
}
